#pragma once
#include<assert.h>


namespace bit
{
	template<class T>


	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;


		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		vector() = default;

		tmplate<class InputIterator>;
		vector(InputIterator first, InputIterator final)
		{
			while (first != finial)
			{
				push_back(*first);
				first++;
			}
		}


		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(int n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(initializer_list n<T>il)
		{
			reserve(il.size());
			for (auto e : il)
			{
				push_back(e);
			}
		}

		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto e : v)
			{
				push_back(e);
			}
		}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}


		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t oldsize = size();
				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, oldsize * sizeof(T));
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + oldsize;
				_end_of_storage = _start + n;
			}
		}

		size_t size()
		{
			return _finish - _start;
		}

		size_t capacity()
		{
			return _end_of_storage - _start;
		}

		T operator[](size_t i)
		{
			assert(i < size());
			return _start[i];
		}

		void push_back(const T& x)
		{
			/*if (_end_of_storage == _finish)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}
			*_finish = x;
			_finish++;*/
			insert(end(), x);
		}
		void pop_back()
		{
			assert(size() > 0);
			_finish--;
		}


		iterator insert(iterator pos, const T& x = T())
		{
			size_t len = pos - _start;
			if (_end_of_storage == _finish)
			{
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + len;
			}
			for (iterator i = end(); i > pos; i--)
			{
				*i = *(i - 1);
			}
			*pos = x;
			_finish++;
			return pos;
		}
		iterator erase(iterator pos)
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			for (iterator it = pos; it < end() - 1; it++)
			{
				*it = *(it + 1);
			}
			_finish--;
			return pos;
		}

		iterator find(const T& x)
		{
			for (iterator it = begin(); it < end(); it++)
			{
				if (*it == x)
				{
					return it;
				}
			}
			return nullptr;
		}


	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};
}